home
diamond Go Premium
Data Engineering Path  ·  PySpark

Streaming Output Modes & Triggers

When working with Structured Streaming, you must specify how and when Spark writes computed results to the output sink. This is controlled by Output Modes and Streaming Triggers.


Streaming Output Modes

The Output Mode defines which part of your streaming DataFrame is written to the sink during each micro-batch trigger:

Output Mode Spark Syntax Description
Append .outputMode("append") The Default Mode. Only writes newly arrived rows to the sink. Guaranteed to never change existing written files.
Update .outputMode("update") Writes only the rows that were updated (or added) since the last trigger. If there are no aggregations, behaves identically to Append.
Complete .outputMode("complete") Writes the entire table (all historical aggregated results) to the sink on every trigger. Requires aggregations (like groupBy); otherwise throws an error!

Streaming Triggers (When to Process)

A Trigger defines the timing of stream evaluations:

# Configure trigger interval
df.writeStream.trigger(processingTime="10 seconds").start()
  • Micro-Batch (Default): If no trigger is specified, Spark runs micro-batches as fast as possible (starts the next one immediately after the previous one finishes).
  • Processing Time: Runs the streaming query at regular clock intervals (e.g. every 10 seconds, 1 minute).
  • Available Now: Processes all available data in the source, writes it, and stops the streaming query automatically (ideal for cost-effective daily/hourly batch-like streaming runs).
  • Continuous Processing: Experimental low-latency engine that processes events record-by-record, achieving sub-millisecond latencies (restricted to simple maps and select transformations).

PySpark Code Example: Aggregations in Complete Mode

Here is a complete script demonstrating event rate aggregations using Complete Mode and processing-time triggers:

from pyspark.sql import SparkSession
from pyspark.sql import functions as F

# 1. Setup Spark
spark = SparkSession.builder \
    .appName("Streaming Output Modes") \
    .master("local[*]") \
    .getOrCreate()

# 2. Ingest Rate Stream (Generates 'timestamp' and 'value' columns automatically)
rate_stream_df = spark.readStream \
    .format("rate") \
    .option("rowsPerSecond", 5) \
    .load()

# 3. Apply Grouping Aggregations (Even/Odd value counts)
aggregated_df = rate_stream_df.withColumn("type", F.when(F.col("value") % 2 == 0, "Even").otherwise("Odd")) \
                              .groupBy("type") \
                              .count()

# 4. Write stream using COMPLETE mode
# This aggregates and updates the complete count table in console every 5 seconds
query = aggregated_df.writeStream \
    .format("console") \
    .outputMode("complete") \
    .trigger(processingTime="5 seconds") \
    .option("checkpointLocation", "complete_checkpoints") \
    .start()

query.awaitTermination()
Find this content helpful? ☕ Buy me a coffee

Entity Details

Create New Item

celebration
Enjoying the free content?

Create a free account to track your progress and save your place.

Create Free Account
help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.